﻿function AttendanceListController($scope, service, $routeParams, $location, $http) {
    /// datepicker

    $scope.openDayFrom = function ($event) {
        $event.preventDefault();
        $event.stopPropagation();

        $scope.openedDayForm = true;
    };
    $scope.initDate = new Date('2016-15-20');
    $scope.formats = ['dd-MMMM-yyyy', 'yyyy/MM', 'dd.MM.yyyy', 'shortDate'];
    $scope.format = $scope.formats[1];
    //end

    $scope.getArray = [{ a: 1, b: 2 }, { a: 3, b: 4 }];
    $scope.month = new Date();

    //get data header csv
    function dataTotalOfMonth(data) {

        return $scope.dataOfTotalOfMonth = data;
    }
    //end
    // data body csv
    function dataCSV(data) {
        var dataBig = [];
        var arrayTitleCSV = ['StampDate', 'CheckIn', 'CheckOut', 'TotalWork', 'TotalBeark', 'TotalGoOut'];
        _.each(data, function (i) {
            _.each(arrayTitleCSV, function (j) {

                if (j == 'StampDate' && dataBig.length >= 6) {
                    dataBig.push('\n' + i[j]);
                }
                else {
                    dataBig.push(i[j]);
                }
            })
        });
        return dataBig;
    }
    //end
    // create csv 
    function createCSV(data) {
        console.log($scope.dataOfTotalOfMonth);
        var arrTotaWork = $scope.dataOfTotalOfMonth;
        var csvText = "Store : " + arrTotaWork.StoreName + "\nStaff Name : " + arrTotaWork.Name + "\nTotal date : " + arrTotaWork.TotalWorkDate + "Total Work : " + arrTotaWork.TotalWork +
            "Total break:" + arrTotaWork.TotalBeark + "Total go out:" + arrTotaWork.TotalGoOut + "\n" + " Date,Check in,Check out, Work time, Break time, Go out time" +
            "\n" +
            dataCSV(data);
        return csvText;
    }
    //end
    /// get data of total of month 
    $http.get(service.locationUrl + "/totalofmonth/companystaffid/" + $routeParams.Id + "/month/" + $scope.month.toString('yyyy-MMM-dd'), {})
        .success(function (data) {
            $scope.totalOfMonth = data[0];
            dataTotalOfMonth(data[0])
        })
        .error(function (err) {

            console.log("error", err);
        });
    //END

    console.log("staffId" + $routeParams.Id)

    //get data of Store list
    $http.get(service.locationUrl + "/totalworkofstaff/companystaffid/" + $routeParams.Id + "/month/" + $scope.month.toString('yyyy-MMM-dd'), {})
        .success(function (data) {
            //data.StampDate = data.StampDate.toString('MMM-dd')
            $scope.listWork = data;
            /// create data for csv
            $scope.ExpCSV = function () {
                // use for japan font
                //var blob = new Blob(["\uFEFF", createCSV()], {});
                var blob = new Blob([createCSV(data)], {});
                //new way
                var csvUrl = URL.createObjectURL(blob);
                console.log("CSvurl", csvUrl);

                $("#expCsv")
                            .attr({
                                'download': "AttendanceStatus.csv",
                                'href': csvUrl
                            });

            }
            //END
        })
        .error(function (err) {

            console.log("error", err);
        });

    $scope.changeTime = function () {
        /// get data of total of month 
        $http.get(service.locationUrl + "/totalofmonth/companystaffid/" + $routeParams.Id + "/month/" + $scope.month.toString('yyyy-MMM-dd'), {})
            .success(function (data) {
                $scope.totalOfMonth = data[0];
                dataTotalOfMonth(data[0])
            })
            .error(function (err) {

                console.log("error", err);
            });
        //END
        /// get data detail of month
        $http.get(service.locationUrl + "/totalworkofstaff/companystaffid/" + $routeParams.Id + "/month/" + $scope.month.toString('yyyy-MMM-dd'), {})
       .success(function (data) {
           //data.StampDate = data.StampDate.toString('MMM-dd')
           $scope.listWork = data;
           // exp csv
           $scope.ExpCSV = function () {
               // use for japan font
               //var blob = new Blob(["\uFEFF", createCSV()], {});
               var blob = new Blob([createCSV(data)], {});
               //new way
               var csvUrl = URL.createObjectURL(blob);
               console.log("CSvurl", csvUrl);

               $("#expCsv")
                           .attr({
                               'download': "AttendanceStatus.csv",
                               'href': csvUrl
                           });

           }
           //END
       })
       .error(function (err) {

           console.log("error", err);
       });

    }
    $scope.edit = function (id, stampdate) {
        var date = stampdate.toString('yyyy-MMM-dd')
        $location.path("/StaffTimeEdit/" + id).search({
            date: date,
            type: 'edit'
        });
    }
}

